package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqCompostCraftMapper;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.SjqCompostCraft;
import com.guofen.service.sjq.SjqCompostCraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqCompostCraftServiceImpl implements SjqCompostCraftService {
    @Autowired
    SjqCompostCraftMapper sjqCompostCraftMapper;

    @Override
    public boolean add(            String rawMaterial1,
                                   String peat1,
                                   String backfillFertilizer1,
                                   String woodMatrix1,
                                   String output1,
                                   String tontankFeed2,
                                   String disposalKitchenWaste2,
                                   String peat2,
                                   String driedKitchenWaste2,
                                   String woodMatrix2,
                                   String dischargeTonBox2,
                                   String kitchenWasteUnderSieve2,
                                   String tontankFeed3,
                                   String disposalKitchenWaste3,
                                   String peat3,
                                   String driedKitchenWaste3,
                                   String woodMatrix3,
                                   String dischargeTonBox3,
                                   String kitchenWasteUnderSieve3,
                                   String tontankFeed4,
                                   String disposalKitchenWaste4,
                                   String peat4,
                                   String driedKitchenWaste4,
                                   String woodMatrix4,
                                   String dischargeTonBox4,
                                   String kitchenWasteUnderSieve4,
                                   String remark){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String eReadingTime = timeFormat.format(now);

        SjqCompostCraft sjqCompostCraft;
        boolean isExist = true;
        if ((sjqCompostCraft = getlast_day()) == null) {
            sjqCompostCraft = new SjqCompostCraft();
            isExist = false;
        }

        sjqCompostCraft.setDate(date);

        if (!rawMaterial1.equals(""))
            sjqCompostCraft.setRawMaterial1(Double.parseDouble(rawMaterial1));
        if (!peat1.equals(""))
            sjqCompostCraft.setPeat1(Double.parseDouble(peat1));
        if (!backfillFertilizer1.equals(""))
            sjqCompostCraft.setBackfillFertilizer1(Double.parseDouble(backfillFertilizer1));
        if (!woodMatrix1.equals(""))
            sjqCompostCraft.setWoodMatrix1(Double.parseDouble(woodMatrix1));
        if (!output1.equals(""))
            sjqCompostCraft.setOutput1(Double.parseDouble(output1));

        if (!tontankFeed2.equals(""))
            sjqCompostCraft.setTontankFeed2(Double.parseDouble(tontankFeed2));
        if (!disposalKitchenWaste2.equals(""))
            sjqCompostCraft.setDisposalKitchenWaste2(Double.parseDouble(disposalKitchenWaste2));
        if (!peat2.equals(""))
            sjqCompostCraft.setPeat2(Double.parseDouble(peat2));
        if (!driedKitchenWaste2.equals(""))
            sjqCompostCraft.setDriedKitchenWaste2(Double.parseDouble(driedKitchenWaste2));
        if (!woodMatrix2.equals(""))
            sjqCompostCraft.setWoodMatrix2(Double.parseDouble(woodMatrix2));
        if (!dischargeTonBox2.equals(""))
            sjqCompostCraft.setDischargeTonBox2(Double.parseDouble(dischargeTonBox2));
        if (!kitchenWasteUnderSieve2.equals(""))
            sjqCompostCraft.setKitchenWasteUnderSieve2(Double.parseDouble(kitchenWasteUnderSieve2));

        if (!tontankFeed3.equals(""))
            sjqCompostCraft.setTontankFeed3(Double.parseDouble(tontankFeed3));
        if (!disposalKitchenWaste3.equals(""))
            sjqCompostCraft.setDisposalKitchenWaste3(Double.parseDouble(disposalKitchenWaste3));
        if (!peat3.equals(""))
            sjqCompostCraft.setPeat3(Double.parseDouble(peat3));
        if (!driedKitchenWaste3.equals(""))
            sjqCompostCraft.setDriedKitchenWaste3(Double.parseDouble(driedKitchenWaste3));
        if (!woodMatrix3.equals(""))
            sjqCompostCraft.setWoodMatrix3(Double.parseDouble(woodMatrix3));
        if (!dischargeTonBox3.equals(""))
            sjqCompostCraft.setDischargeTonBox3(Double.parseDouble(dischargeTonBox3));
        if (!kitchenWasteUnderSieve3.equals(""))
            sjqCompostCraft.setKitchenWasteUnderSieve3(Double.parseDouble(kitchenWasteUnderSieve3));

        if (!tontankFeed4.equals(""))
            sjqCompostCraft.setTontankFeed4(Double.parseDouble(tontankFeed4));
        if (!disposalKitchenWaste4.equals(""))
            sjqCompostCraft.setDisposalKitchenWaste4(Double.parseDouble(disposalKitchenWaste4));
        if (!peat4.equals(""))
            sjqCompostCraft.setPeat4(Double.parseDouble(peat4));
        if (!driedKitchenWaste4.equals(""))
            sjqCompostCraft.setDriedKitchenWaste4(Double.parseDouble(driedKitchenWaste4));
        if (!woodMatrix4.equals(""))
            sjqCompostCraft.setWoodMatrix4(Double.parseDouble(woodMatrix4));
        if (!dischargeTonBox4.equals(""))
            sjqCompostCraft.setDischargeTonBox4(Double.parseDouble(dischargeTonBox4));
        if (!kitchenWasteUnderSieve4.equals(""))
            sjqCompostCraft.setKitchenWasteUnderSieve4(Double.parseDouble(kitchenWasteUnderSieve4));

        if (!remark.equals(""))
            sjqCompostCraft.setRemark(sjqCompostCraft.getRemark()+"\n"+remark);

        int result;
        if (!isExist)
            result = sjqCompostCraftMapper.insertSelective(sjqCompostCraft);
        else
            result = sjqCompostCraftMapper.updateByPrimaryKeySelective(sjqCompostCraft);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqCompostCraft getlast_day(){
        return sjqCompostCraftMapper.getlast();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqCompostCraft> list = sjqCompostCraftMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqCompostCraft> list = sjqCompostCraftMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqCompostCraftMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqCompostCraft record) {
        return sjqCompostCraftMapper.updateByPrimaryKeySelective(record);
    }

}
